package com.xuetang9.t9.hotel.services.impl;

import com.xuetang9.t9.hotel.beans.Guest;
import com.xuetang9.t9.hotel.mapper.IGuestMapper;
import com.xuetang9.t9.hotel.mapper.condition.ICondition;
import com.xuetang9.t9.hotel.mapper.condition.impl.GuestCondition;
import com.xuetang9.t9.hotel.services.IGuestService;
import com.xuetang9.t9.hotel.utils.IsNull;
import com.xuetang9.t9.hotel.utils.OverallConstant;
import com.xuetang9.t9.hotel.utils.ResponseResult;
import com.xuetang9.t9.hotel.utils.SnowFlake;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class GuestServiceImpl implements IGuestService {
    @Autowired
    private IGuestMapper guestMapper;
    @Autowired
    private SnowFlake snowFlake;

    @Override
    public ResponseResult save(Guest guest) {
        if(null == guest){
            return null;
        }
        if(IsNull.isStringNull(guest.getGuestName())){ return null;}
        if(IsNull.isStringNull(guest.getCardNum())) {return null;}

        Guest guestBynum = queryByNum(guest.getCardNum());
        if(guestBynum != null) {
            guestMapper.update(guest);
            return ResponseResult.okResult(guestBynum.getGuestId());
        }

        guest.setGuestId(snowFlake.nextId());
        return ResponseResult.okResult(guest.getGuestId());
    }

    @Override
    public ResponseResult remove(Integer id) {
        if(id == null){
            return ResponseResult.okResult(400,"参数错误");
        }
        return ResponseResult.okResult(200,"成功");
    }

    @Override
    public ResponseResult alter(Guest arrive) {
        return null;
    }

    @Override
    public ResponseResult queryByAll(GuestCondition condition) {
        if(condition == null){
            condition = new GuestCondition(0, OverallConstant.DEFAULT_PAGESIZE);
        }
        if(condition.getPageSize() == null || condition.getPageSize() <= 0){
            condition.setPageSize(OverallConstant.DEFAULT_PAGESIZE);
        }
        if(condition.getCurrPage() == null || condition.getCurrPage() < 1){
            condition.setCurrPage(1);
        }
        return ResponseResult.okResult(guestMapper.selectByAll(condition));
    }

    @Override
    public Long queryByCount(GuestCondition condition) {
        return guestMapper.selectByCount(condition);
    }

    @Override
    public Guest queryByNum(String num) {
        return guestMapper.selectByNum(num);
    }
}
